package com.hundsun.yr.universal.p2p.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.common.PackageUtils;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.KeyBoardListener;
import com.hundsun.yr.universal.p2p.utils.Logs;

import de.greenrobot.event.EventBus;
import android.widget.AdapterView.OnItemClickListener;
/**
 * Created by Administrator on 2018/4/28.
 *
 */

public class WebBindCarActivty extends BaseMineWebActivity implements View.OnClickListener {
    /**
     * 支持放大缩小
     */
    public static final String supportZoom = "supportZoom";
    private boolean supportZoomFlag;
    private boolean flag;
    public HSSharedPreferences sharedPreferences = null;
    /**
     * 目标跳转地址
     */
    private String flagUrl = "";
    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {
        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        supportZoomFlag = extras.getBoolean(supportZoom);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initViewsAndEvents() {
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        mToolBar.setNavigationOnClickListener(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        KeyBoardListener.getInstance(this).init();

        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                Logs.e("ReceivedTitle: " + title);
                toolbar_title.setMaxLines(1);
                toolbar_title.setMaxEms(7);
                toolbar_title.setEllipsize(TextUtils.TruncateAt.END);
                if (title != null && !title.isEmpty()){
                    toolbar_title.setText(title);
                }
            }
        };
        mBrowserLayout.getWebView().setWebChromeClient(wvcc);

        if (supportZoomFlag) {
            mBrowserLayout.mWebView.getSettings().setSupportZoom(supportZoomFlag);
            mBrowserLayout.mWebView.getSettings().setBuiltInZoomControls(supportZoomFlag);
        }


        mBrowserLayout.getWebView().getSettings().setJavaScriptEnabled(true);
        mBrowserLayout.getWebView().addJavascriptInterface(WebBindCarActivty.this, "wjj");
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 网页 返回
     */
    public void webBack() {
        if (mBrowserLayout.canGoBack()) {
            mBrowserLayout.goBack();
        } else {
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            webBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View view) {
        finish();
    }

    /**
     * 绑卡页面
     */
    @JavascriptInterface
    public void rebindcard(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refresh();
            }
        });
    }

    /**
     * 项目列表
     */
    @JavascriptInterface
    public void toInvest(){
        EventBus.getDefault().post(new EventCenter(Extras.RECHARGE_SUCCESS_GO_TO_PROJECT));
        if (BaseAppManager.getInstance().getActivity(InvestDetailsActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(InvestDetailsActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineAssetsActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineAssetsActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineBankCardActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineBankCardActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
        }


        finish();
    }

    /**
     * 充值页面
     */
    @JavascriptInterface
    public void gotoHomepage(){
        if (BaseAppManager.getInstance().getActivity(InvestDetailsActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(InvestDetailsActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineAssetsActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineAssetsActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineBankCardActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineBankCardActivity.class).finish();
        }

        if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null) {
            BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
        }

        if (!TextUtils.equals(sharedPreferences.getShareProf(UserInfo.IF_BINDCARD), "YES")) {
            new HSToast(mContext).showToast(mContext.getString(R.string.tips_openaccount));
        } else {
            Bundle bundle = new Bundle();
            bundle.putBoolean("MINERECHARGE", true);
            readyGo(RechangeNewActivity.class, bundle);
        }
        finish();
    }

}
